<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('frmproducttype_loadGrid');      
$objXajax->registerFunction('frmproducttype_save');
$objXajax->registerFunction('frmproducttype_edit');
$objXajax->registerFunction('frmproducttype_delete');
$objXajax->registerFunction('frmproducttype_fillMenuParent');

function frmproducttype_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();     
    $menu_type = isset($params['parent'])?0:1;
    $name_vi = $params['txtnamemenuVi'];
    $name_en = $params['txtnamemenuEn'];
    $parent_menu = $params['cbbMenuParent'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        if($menu_type==0){
            $sql .="UPDATE tbl_product_type$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_en',parent_id = id,
                menu_type = '$menu_type',user_update = '$user_id',date_update = now() WHERE id =$id";            
        }else{
            if($parent_menu==$id){
             $objResponse->addAlert('Vui lòng chọn menu gốc khác!');   
            }else{
                $sql .="UPDATE tbl_product_type$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_en',parent_id = '$parent_menu',
                menu_type = '$menu_type',user_update = '$user_id',date_update = now() WHERE id =$id"; 
            }
            
        }        
        $objDbSelect->Execute($sql);
        
    }else{
          $sql.= " INSERT INTO tbl_product_type$tbl_postfix(name_vi,name_en,parent_id,menu_type,active,user_created,date_created,user_update,date_update)
    VALUES('$name_vi','$name_en','$parent_menu','$menu_type','1','$user_id',now(),'$user_id',now())"; 
           $objDbSelect->Execute($sql); 
        if ($menu_type==0){
            $parent_id = mysql_insert_id();
            $sql = " UPDATE tbl_product_type$tbl_postfix SET parent_id = '$parent_id' WHERE id = $parent_id";
            $objDbSelect->Execute($sql);        
        }
    }    
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}
function frmproducttype_fillMenuParent()
{
    global $objDbSelect,$lang,$tbl_postfix;;
    $objResponse = new xajaxResponse();    
    $sql = " SELECT id, name_$lang as name FROM tbl_product_type$tbl_postfix WHERE 
        menu_type = 0 and id not in (select distinct parent_id from tbl_product$tbl_postfix) ";    
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $temp ='';
    for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
    }
    $temp =  '<select id="frmproducttype_cbbMenuParent" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $objResponse->addAssign('frmproducttype_tdmenuparent', 'innerHTML',$temp);
    return $objResponse->getXML();
}
function frmproducttype_loadGrid()
{     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmproducttype_getGrid();    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_producttype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmproducttype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmproducttype_tbl');");                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmproducttype_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select pt1.id,pt1.active, pt1.name_vi,pt1.name_en,if(pt1.menu_type=0,'Gốc','Trung Gian') as menu_type,pt2.name_$lang parent_name from tbl_product_type$tbl_postfix as pt1 LEFT JOIN 
    tbl_product_type$tbl_postfix as pt2 ON pt1.parent_id = pt2.id  order by pt1.name_$lang ";
    return $objDbSelect->GetArray($sqlSelect);
}  

    function frmproducttype_edit($id){
        global $objDbSelect,$tbl_postfix;;
        $objResponse = new xajaxResponse();
        $sql = " SELECT * FROM tbl_product_type$tbl_postfix WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi'];
        $name_en   = $arr[0]['name_en'];    
        $parent_id     = $arr[0]['parent_id'];
        $menu_type         = $arr[0]['menu_type'];
        $arrMid = array();
        if($menu_type==0){
            $objResponse->addScript("$('#frmproducttype_parent').attr('checked',true);");
            $objResponse->addScript("$('#frmproducttype_trmenuparent').css('display','none');");
            $sql = "SELECT id FROM tbl_product_type$tbl_postfix WHERE parent_id = '$id' and id <>'$id'";
            $arrMid=$objDbSelect->GetArray($sql);
        }else{
            $objResponse->addScript("$('#frmproducttype_note').attr('checked',true);");
            $objResponse->addScript("$('#frmproducttype_trmenuparent').css('display','');");
            $objResponse->addScript("$('#frmproducttype_cbbMenuParent').val('$parent_id');");
            $objResponse->addScript("$('#frmproducttype_note').removeAttr('disabled');"); 
        }      
        $objResponse->addScript("$('#frmproducttype_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#frmproducttype_txtnamemenuEn').val('$name_en');");
        $objResponse->addScript('changeWaiting(1);');
        if(count($arrMid)>0){
          $objResponse->addScript("$('#frmproducttype_note').attr('disabled',true)");  
        }
        return $objResponse->getXML();   
    }
    
    function frmproducttype_delete($id){
        global $objDbSelect,$tbl_postfix;
        $sql = "delete from tbl_product$tbl_postfix where parent_id = '$id' or parent_id in (select id from tbl_product_type$tbl_postfix where parent_id in ($id))";
        $result = $objDbSelect->Execute($sql);
        $sql = "delete from tbl_product_type$tbl_postfix where id IN ($id)";
        $result = $objDbSelect->Execute($sql); 
        $objResponse = new xajaxResponse();
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();
    }
    
?>
